<template>
	<view class="content">
		<view class="form">
			<view class="item">
				<view class="label">
					<image class="starImg" src="../../static/img/star.png" mode=""></image>
					机构名称
				</view>
				<view class="ipt">
					<input type="text" placeholder="请输入机构名称" v-model="info.school_name">
				</view>
			</view>
			<view class="item">
				<view class="label">
					<image class="starImg" src="../../static/img/star.png" mode=""></image>
					公司名称
				</view>
				<view class="ipt">
					<input type="text" placeholder="请输入公司名称" v-model="info.company_name">
				</view>
			</view>
			<view class="item">
				<view class="label">
					<image class="starImg" src="../../static/img/star.png" mode=""></image>
					负责人
				</view>
				<view class="ipt">
					<input type="text" placeholder="请输入负责人姓名" v-model="info.contact_name">
				</view>
			</view>
			<view class="item">
				<view class="label">
					<image class="starImg" src="../../static/img/star.png" mode=""></image>
					手机号
				</view>
				<view class="ipt">
					<input type="text" placeholder="请输入负责人手机号" v-model="info.contact_phone">
				</view>
			</view>
			<view class="item">
				<view class="label">
					<image class="starImg" src="../../static/img/star.png" mode=""></image>
					身份证号
				</view>
				<view class="ipt">
					<input type="text" placeholder="请输入负责人身份证号" v-model="info.id_card_number">
				</view>
			</view>
			<view class="item">
				<view class="label">
					<view class="starImg"></view>
					邮箱
				</view>
				<view class="ipt">
					<input type="text" placeholder="请输入邮箱" v-model="info.mailbox">
				</view>
			</view>
			<view class="item">
				<view class="label">
					<image class="starImg" src="../../static/img/star.png" mode=""></image>
					银行账号
				</view>
				<view class="ipt">
					<input type="number" placeholder="请输入银行账号" v-model="info.bank_account">
				</view>
			</view>
			<view class="item">
				<view class="label">
					<image class="starImg" src="../../static/img/star.png" mode=""></image>
					业务员
				</view>
				<view class="ipt">
					<input type="text" placeholder="请输入业务员姓名" v-model="info.salesman">
				</view>
			</view>
			<view class="item">
				<view class="label">
					<image class="starImg" src="../../static/img/star.png" mode=""></image>
					级别
				</view>
				<view class="ipt" @tap="levelShow = true">
					<view class="text">
						{{levelText}}
					</view>
					<image class="lowerImg" src="../../static/img/lowerNew.png" mode=""></image>
				</view>
			</view>
			<view class="item" @tap="join_time_show = true">
				<view class="label">
					<image class="starImg" src="../../static/img/star.png" mode=""></image>
					加盟时间
				</view>
				<view class="ipt">
					{{join_time}}
				</view>
			</view>
			<view class="item">
				<view class="label">
					<image class="starImg" src="../../static/img/star.png" mode=""></image>
					所在区域
				</view>
				<view class="ipt">
					<input type="text" placeholder="请输入所在区域" v-model="info.area">
				</view>
			</view>
			<view class="item">
				<view class="label">
					<image class="starImg" src="../../static/img/star.png" mode=""></image>
					详细地址
				</view>
				<view class="ipt">
					<input type="text" placeholder="请输入详细地址" v-model="info.address">
				</view>
			</view>
			<view class="item">
				<view class="label">
					<image class="starImg" src="../../static/img/star.png" mode=""></image>
					渠道
				</view>
				<view class="ipt" @tap="fromShow = true">
					<view class="text">
						{{fromText}}
					</view>
					<image class="lowerImg" src="../../static/img/lowerNew.png" mode=""></image>
				</view>
			</view>
			<view class="item" @tap="showArea()">
				<view class="label">
					<view class="starImg"></view>
					受保护区域
				</view>
				<view class="ipt">
					{{protected_area}}
				</view>
			</view>

		</view>
		<view class="btn" @tap="addSchool()">
			确认提交
		</view>
		<!-- 级别下拉框 -->
		<u-picker :show="levelShow" :columns="levelColumns" keyName="label" @confirm="confirmLevel"
			@cancel="levelShow = false"></u-picker>
		<!-- 渠道下拉框 -->
		<u-picker :show="fromShow" :columns="fromColumns" keyName="label" @confirm="confirmFrom"
			@cancel="fromShow = false"></u-picker>
		<u-datetime-picker :show="join_time_show" v-model="info.join_time" mode="date" @confirm="confirm_join_time"
			@cancel="cancel_join_time"></u-datetime-picker>
		<ba-tree-picker ref="treePicker" :multiple='true' :selectParent='false' @select-change="selectChange" title="选择城市"
			:localdata="options" valueKey="id" textKey="name" childrenKey="sub" :selectedData="currentData"/>
	</view>
</template>

<script>
	import baTreePicker from "@/components/ba-tree-picker/ba-tree-picker.vue"
	export default {
		components: {
			baTreePicker
		},
		data() {
			return {
				options: [],
				currentData: [],
				protected_area: "请选择受保护区域",
				levelShow: false,
				levelColumns: [
					[{
						label: '总部',
						id: 1
					}, {
						label: '省',
						id: 2
					}, {
						label: '省会',
						id: 3
					}, {
						label: '地级市',
						id: 4
					}, {
						label: '区/县',
						id: 5
					}, {
						label: '单店一',
						id: 6
					}, {
						label: '单店二',
						id: 7
					}, {
						label: '单店三',
						id: 8
					}]
				],
				levelText: "请选择级别",
				join_time_show: false,
				fromShow: false,
				fromColumns: [
					[{
						label: '个人',
						id: 1
					}, {
						label: '客户介绍',
						id: 2
					}, {
						label: '招商会',
						id: 3
					}]
				],
				fromText: "请选择渠道",
				join_time: "请选择加盟时间",
				info: {
					school_name: '',
					company_name: '',
					contact_name: '',
					id_card_number: '',
					contact_phone: '',
					mailbox: '',
					bank_account: '',
					salesman: '',
					level: 0,
					join_time: Number(new Date()),
					area: '',
					address: '',
					from: '',
					protected_area: '',
					school_id: ''
				},
			};
		},
		onLoad(option) {
			this.info.school_id = option.school_id
			this.getData(option.school_id)
			this.getRegion();
		},
		methods: {
			async getData(school_id) {
				const {
					data: res
				} = await uni.$http.post('school/school/getSchoolDetail', {
					school_id: school_id
				})
				if (res.code != 1) {
					return this.toast.msg(res.msg)
				}
				this.info = res.data;
				
				this.join_time = res.data.join_time;
				this.protected_area = res.data.cn_protected_area;
				this.currentData = res.data.arr_protected_area;
			
				if (res.data.level == 1) {
					this.levelText = '总部'
				}
				if (res.data.level == 2) {
					this.levelText = '省'
				}
				if (res.data.level == 3) {
					this.levelText = '省会'
				}
				if (res.data.level == 4) {
					this.levelText = '地级市'
				}
				if (res.data.level == 5) {
					this.levelText = '区/县'
				}
				if (res.data.level == 6) {
					this.levelText = '单店一'
				}
				if (res.data.level == 7) {
					this.levelText = '单店二'
				}
				if (res.data.level == 8) {
					this.levelText = '单店三'
				}
				
				if (res.data.from == 1) {
					this.fromText = '个人'
				}
				if (res.data.from == 2) {
					this.fromText = '客户介绍'
				}
				if (res.data.from == 3) {
					this.fromText = '招商会'
				}
			},
			showArea() {
				this.$refs.treePicker._show();
			},
			selectChange(ids, names) {
				this.protected_area = names;
				this.info.protected_area = JSON.stringify(ids);
			},
			async getRegion() {
				const {
					data: res
				} = await uni.$http.post('school/school/getRegion')
				if (res.code != 1) {
					return this.toast.msg(res.msg)
				}
				this.options = res.data;
			},
			confirm_join_time(e) {
				var time = this.timestampToDate(e.value);
				this.info.join_time = time
				this.join_time = time
				this.join_time_show = false
			},
			cancel_join_time() {
				this.join_time_show = false
			},
			timestampToDate(timestamp) {
				var date = new Date(timestamp); // 如果timestamp是数值，则直接使用，否则需要转换
				var year = date.getFullYear();
				var month = (date.getMonth() + 1).toString().padStart(2, '0');
				var day = date.getDate().toString().padStart(2, '0');
				return `${year}-${month}-${day}`;
			},
			confirmLevel(e) {
				this.levelText = e.value[0].label
				this.info.level = e.value[0].id
				this.levelShow = false
			},
			confirmFrom(e) {
				this.fromText = e.value[0].label
				this.info.from = e.value[0].id
				this.fromShow = false
			},
			async addSchool() {
				const {
					data: res
				} = await uni.$http.post('school/school/editSchool', this.info)
				if (res.code != 1) {
					return this.toast.msg(res.msg)
				}
				this.toast.msg('编辑成功')
				setTimeout(() => {
					uni.navigateTo({
						url: '../school/index'
					})
				}, 1000)
			}
		}
	}
</script>

<style lang="scss">
	.content {
		min-height: 100vh;

		.form {
			width: 652rpx;
			margin: 40rpx auto;
			padding-bottom: 100rpx;

			.item {
				display: flex;
				align-items: center;
				padding: 0 35rpx 0 19rpx;
				margin-bottom: 30rpx;
				width: 652rpx;
				height: 78rpx;
				line-height: 78rpx;
				border-radius: 10rpx;
				border: 2rpx solid #FCF7F7;

				.label {
					display: flex;
					align-items: center;
					font-size: 28rpx;
					margin-right: 70rpx;
					width: 160rpx;

					.starImg {
						width: 14rpx;
						height: 42rpx;
						margin-right: 15rpx;
					}
				}

				.ipt {
					flex: 1;
					position: relative;
					display: flex;
					align-items: center;
					color: #666;

					.lowerImg {
						position: absolute;
						right: 0;
						width: 20rpx;
						height: 11rpx;
					}
				}

				.time {
					color: #666;
					display: flex;
					align-items: center;

					.timeImg {
						width: 35rpx;
						height: 35rpx;
						margin-right: 17rpx;

					}
				}
			}

			.textBox {
				padding: 0 35rpx 0 19rpx;
				display: flex;
				border: 2rpx solid #FCF7F7;

				.label {
					height: 70rpx;
					display: flex;
					align-items: center;
					font-size: 28rpx;
					margin-right: 70rpx;
					width: 160rpx;

					.starImg {
						width: 14rpx;
						height: 42rpx;
						margin-right: 15rpx;
					}
				}

				.ipt {
					flex: 1;
					height: 352rpx;

					.u-textarea {
						height: 352rpx;
						border: none;
						padding-left: 0;

						/deep/.u-textarea__field {
							height: 352rpx !important;
						}

						.uni-textarea-textarea {
							height: 352rpx;
						}
					}
				}
			}
		}

		.btn {
			position: fixed;
			bottom: 50rpx;
			left: 50%;
			transform: translateX(-50%);
			width: 652rpx;
			height: 78rpx;
			line-height: 78rpx;
			text-align: center;
			background: #CCCCCC;
			border: 2rpx solid #69A1E0;
		}
	}
</style>